//
//  MZTViewModel.swift
//  RxMoya
//
//  Created by XuHao on 2018/4/26.
//  Copyright © 2018年 XuHao. All rights reserved.
//

import Foundation
import RxSwift
import RxCocoa
import Moya
import Kingfisher

import MJRefresh


let cellID = "MZTCell"


enum MZTRefreshStatus {
    case none
    case beginHeaderRefresh
    case endHeaderRefresh
    case beginFooterRefresh
    case endFooterRefresh
    case noMoreData
}

class MZTViewModel: NSObject {
    
    var bag:DisposeBag = DisposeBag.init()
    
    let modelObserable = Variable<[MZTModel]>([])
    
    let requestConmond = PublishSubject<Bool>()
    
    var tableView = UITableView()
    
    let provider = MoyaProvider<MZTAPI>()
    
    var refreshobserable = Variable<MZTRefreshStatus>(.none)
    
    func binding()  {
        
        
        modelObserable.asObservable().bind(to: tableView.rx.items(cellIdentifier: cellID, cellType: MZTCell.self)){ row,  model , cell in
            
            cell.titleLabel.text = model.title
            cell.mzImageVIew.kf.setImage(with: URL.init(string: model.img!))
            
        }.disposed(by: bag)
        
    
        requestConmond.subscribe { (event: Event<Bool>) in
            
            self.provider.rx.request(.GetMz)
            .mapJSON().asObservable().mapArray(type: MZTModel.self)
                .subscribe(onNext: { (model) in
                    
                    if event.element! {
                        self.modelObserable.value = model
                        self.refreshobserable.value = .endHeaderRefresh
                    }
                    else {
                        self.modelObserable.value += model
                        self.refreshobserable.value = .endFooterRefresh
                    }
                    
                }, onError: { (error) in
                    if event.element! {
                        
                        self.refreshobserable.value = .endHeaderRefresh
                    }
                    else {                        
                        self.refreshobserable.value = .endFooterRefresh
                    }
                    
                }, onCompleted: nil, onDisposed: nil)
            .disposed(by: self.bag)
        }.disposed(by: self.bag)
        
        
        refreshobserable.asObservable().subscribe(onNext: { (status) in
            
            switch status {
                
            case .beginHeaderRefresh:
                self.tableView.mj_header.beginRefreshing()
            case .endHeaderRefresh:
                self.tableView.mj_header.endRefreshing()
                self.tableView.mj_footer.resetNoMoreData()
            case .beginFooterRefresh:
                self.tableView.mj_footer.beginRefreshing()
            case .endFooterRefresh:
                self.tableView.mj_footer.endRefreshing()
            case .noMoreData:
                self.tableView.mj_footer.endRefreshingWithNoMoreData()
            default:
                break
            }
            
        }, onError: nil, onCompleted: nil, onDisposed: nil).disposed(by: bag)
        
    }
    
    
    
}



